package com.wyzz.hr.action;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.Constants;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.base.Status;
import com.wyzz.hr.bo.ICompaplanBo;
import com.wyzz.hr.bo.IEmpSalaryAcctitemsBo;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.IEmpsalaryperiodBo;
import com.wyzz.hr.bo.ISalaryconfBo;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empsalaryacctitems;
import com.wyzz.hr.pojo.Empsalaryadj;
import com.wyzz.hr.pojo.Empsalaryconfig;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.DateUtil;

public class DWRforSearchBatchCompaplan extends BaseAction implements Status,
		Constants {

	public DWRforSearchBatchCompaplan() {
		esa = new Empsalaryadj();
		esc = new Empsalaryconfig();
	}

	private Empsalaryadj getCompaByConfig(Empsalaryconfig config,
			Empsalaryconfig newConfig, Empsalaryadj esa) throws Exception {
		Class ownerClass = config.getClass();
		Class adjClass = esa.getClass();
		for (int i = 1; i <= 48; i++) {
			Method escMethod = ownerClass.getMethod((new StringBuilder())
					.append("getEscColumn").append(i).toString(), new Class[0]);
			Method adjMethod = adjClass.getMethod((new StringBuilder()).append(
					"setEsaColumn").append(i).append("Cur").toString(),
					new Class[] { BigDecimal.class });
			adjMethod.invoke(esa, new Object[] { (BigDecimal) escMethod.invoke(
					config, new Object[0]) });
			escMethod = ownerClass.getMethod((new StringBuilder()).append(
					"getEscColumn").append(i).toString(), new Class[0]);
			adjMethod = adjClass.getMethod((new StringBuilder()).append(
					"setEsaColumn").append(i).append("Pro").toString(),
					new Class[] { BigDecimal.class });
			adjMethod.invoke(esa, new Object[] { (BigDecimal) escMethod.invoke(
					newConfig, new Object[0]) });
		}

		esa.setEsaJobgradeCur(config.getEscJobgrade());
		esa.setEsaJobgradePro(newConfig.getEscJobgrade());
		esa.setEsaEsavIdCur(config.getEscEsavId());
		esa.setEsaEsavIdPro(newConfig.getEscEsavId());
		return esa;
	}

	public String deleteCompaplan() {
		if (StringUtils.isEmpty(compaplanIds))
			return "error";
		ICompaplanBo compaplanBo = (ICompaplanBo) getBean("compaplanBo");
		Empsalaryadj compaplanFromDB = compaplanBo.loadCompaplan(compaplanIds,
				new String[] { "esaEmpno" });
		if (compaplanFromDB == null)
			return "error";
		String empName = compaplanFromDB.getEsaEmpno().getEmpName();
		if (compaplanBo.deleteCompaplan(compaplanFromDB)) {
			addActionMessage((new StringBuilder()).append(
					"\u5220\u9664\u5458\u5DE5").append(empName).append(
					"\u7684\u8C03\u85AA\u8BA1\u5212\u6210\u529F\uFF01")
					.toString());
			return "success";
		} else {
			addActionMessage((new StringBuilder()).append(
					"\u5220\u9664\u5458\u5DE5").append(empName).append(
					"\u7684\u8C03\u85AA\u8BA1\u5212\u5931\u8D25\uFF01")
					.toString());
			return "error";
		}
	}

	public List getAcctItemsById(String acctversionId, String configId)
			throws Exception {
		String hasAuth = DWRUtil.checkAuth("searchBatchCompaplan", "execute");
		if ("error".equals(hasAuth))
			return null;
		IEmpSalaryAcctitemsBo acctItemBo = (IEmpSalaryAcctitemsBo) SpringBeanFactory
				.getBean("empsalaryacctitemsBo");
		ISalaryconfBo salaryconf = (ISalaryconfBo) getBean("salaryconfBo");
		Empsalaryconfig config = salaryconf.loadSalaryconf(configId,
				new String[0]);
		config.decryEmpSalaryConf(config);
		List acctList = acctItemBo.getItemsByAcctversion(acctversionId);
		if (config != null) {
			Class ownerClass = config.getClass();
			Empsalaryacctitems esai;
			Method escMethod;
			for (Iterator i$ = acctList.iterator(); i$.hasNext(); esai
					.setItemValue((BigDecimal) escMethod.invoke(config,
							new Object[0]))) {
				esai = (Empsalaryacctitems) i$.next();
				escMethod = ownerClass.getMethod((new StringBuilder()).append(
						"getEscColumn").append(esai.getEsaiDataSeq())
						.toString(), new Class[0]);
			}

		}
		return acctList;
	}

	public List getConfigItemsById(String configId) {
		String hasAuth = DWRUtil.checkAuth("searchSalaryPaid", "execute");
		if ("error".equals(hasAuth)) {
			return null;
		} else {
			ISalaryconfBo salaryconf = (ISalaryconfBo) getBean("salaryconfBo");
			return salaryconf.getConfigItemsById(configId);
		}
	}

	public String addEmpsalaryadj() throws Exception {
		String currentUserNo = getCurrentEmpNo();
		System.out.println(esa.getId());
		String empId = esa.getEsaEmpno().getId();
		IEmployeeBo employeeBo = (IEmployeeBo) SpringBeanFactory
				.getBean("empBo");
		Employee emp = employeeBo.loadEmp(empId, new String[] { "config" });
		if (emp == null || emp.getConfig() == null)
			return "error";
		IEmpsalaryperiodBo periodBo = (IEmpsalaryperiodBo) SpringBeanFactory
				.getBean("empsalaryperiodBO");
		String yearMonthArr[] = curEffDate.split("-");
		Integer periodStatus = periodBo.getEspdStatusNew((new StringBuilder())
				.append(yearMonthArr[0]).append(yearMonthArr[1]).toString());
		if (periodStatus != null && periodStatus.intValue() == 2) {
			if (yearMonthArr[1].charAt(0) == '0')
				yearMonthArr[1] = yearMonthArr[1].substring(1);
			addErrorInfo((new StringBuilder())
					.append(yearMonthArr[0])
					.append("\u5E74")
					.append(yearMonthArr[1])
					.append(
							"\u6708\u85AA\u8D44\u53D1\u653E\u5DF2\u5C01\u5E10\uFF0C\u4E0D\u5141\u8BB8\u8C03\u6574\uFF01")
					.toString());
			return "success";
		}
		String empName = emp.getEmpName();
		java.util.Date date = (new SimpleDateFormat("yyyy-MM-dd"))
				.parse((new StringBuilder()).append(curEffDate).append("-")
						.append("1").toString());
		ICompaplanBo compaplanBo = (ICompaplanBo) getBean("compaplanBo");
		Empsalaryadj ajs = compaplanBo.loadCompaplanInfoByEmpNo(empId, date);
		if (ajs != null) {
			String dateArr[] = DateUtil.formatDateToS(ajs.getEsaCurEffDate(),
					"yyyy-MM").split("-");
			addErrorInfo((new StringBuilder())
					.append("\u5458\u5DE5")
					.append(empName)
					.append(dateArr[0])
					.append("\u5E74")
					.append(dateArr[1])
					.append(
							"\u6708\u5DF2\u7ECF\u6709\u4E00\u6761\u751F\u6548\u7684\u8C03\u85AA\u8BA1\u5212\uFF0C\u64CD\u4F5C\u5931\u8D25\uFF01")
					.toString());
			return "success";
		}
		Empsalaryconfig config = emp.getConfig();
		config.decryEmpSalaryConf(config);
		esa.setEsaCurEffDate(date);
		esa = getCompaByConfig(config, esc, esa);
		BigDecimal incrRate[] = calIncreace(config, esc);
		esa.setEsaCurIncrRate(incrRate[0]);
		esa.setEsaCurIncrRate1(incrRate[1]);
		Empsalaryadj compaplan = esa;
		Empsalaryadj compaplanFromDB = compaplanBo
				.loadCompaplanInfoByEmpNo(empId);
		if (compaplanFromDB == null) {
			compaplan.setEsaStatus(Integer.valueOf(11));
			if (compaplanBo.insertCompaplan(compaplan, currentUserNo)) {
				addActionMessage((new StringBuilder()).append(
						"\u6DFB\u52A0\u5458\u5DE5").append(empName).append(
						"\u7684\u8C03\u85AA\u8BA1\u5212\u6210\u529F\uFF01")
						.toString());
				return "success";
			} else {
				addErrorInfo((new StringBuilder()).append(
						"\u6DFB\u52A0\u5458\u5DE5").append(empName).append(
						"\u7684\u8C03\u85AA\u8BA1\u5212\u51FA\u9519\uFF01")
						.toString());
				return "error";
			}
		}
		try {
			compaplanFromDB.decryEmpSalaryAdj(compaplanFromDB);
			compaplanBo.updateCompaplan(compaplan, compaplanFromDB,
					currentUserNo);
			addActionMessage((new StringBuilder()).append(
					"\u4FEE\u6539\u5458\u5DE5").append(empName).append(
					"\u7684\u8C03\u85AA\u8BA1\u5212\u6210\u529F\uFF01")
					.toString());
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
		}
		addErrorInfo((new StringBuilder()).append("\u4FEE\u6539\u5458\u5DE5")
				.append(empName).append(
						"\u7684\u8C03\u85AA\u8BA1\u5212\u51FA\u9519\uFF01")
				.toString());
		return "error";
	}

	private BigDecimal[] calIncreace(Empsalaryconfig config,
			Empsalaryconfig newConfig) throws Exception {
		BigDecimal rs[] = { new BigDecimal(0), new BigDecimal(0) };
		BigDecimal bs = new BigDecimal(0);
		BigDecimal newbs = new BigDecimal(0);
		BigDecimal beforTax = new BigDecimal(0);
		BigDecimal beforTaxNew = new BigDecimal(0);
		IEmpSalaryAcctitemsBo acctItemBo = (IEmpSalaryAcctitemsBo) SpringBeanFactory
				.getBean("empsalaryacctitemsBo");
		List acctList = acctItemBo.getItemsByAcctversion(config.getEscEsavId()
				.getId());
		Empsalaryacctitems item = new Empsalaryacctitems();
		Class ownerClass = config.getClass();
		for (int i = 0; i < acctList.size(); i++) {
			item = (Empsalaryacctitems) acctList.get(i);
			Method escMethod = ownerClass.getMethod((new StringBuilder())
					.append("getEscColumn").append(item.getEsaiDataSeq())
					.toString(), new Class[0]);
			if (item.getEsaiEsdd().getEsddDataType().intValue() == 1) {
				bs = (BigDecimal) escMethod.invoke(config, new Object[0]);
				continue;
			}
			if (item.getEsaiEsdd().getEsddDataType().intValue() == 8)
				beforTax = (BigDecimal) escMethod.invoke(config, new Object[0]);
		}

		acctList = acctItemBo.getItemsByAcctversion(newConfig.getEscEsavId()
				.getId());
		ownerClass = newConfig.getClass();
		for (int i = 0; i < acctList.size(); i++) {
			item = (Empsalaryacctitems) acctList.get(i);
			Method escMethod = ownerClass.getMethod((new StringBuilder())
					.append("getEscColumn").append(item.getEsaiDataSeq())
					.toString(), new Class[0]);
			if (item.getEsaiEsdd().getEsddDataType().intValue() == 1) {
				newbs = (BigDecimal) escMethod.invoke(newConfig, new Object[0]);
				continue;
			}
			if (item.getEsaiEsdd().getEsddDataType().intValue() == 8)
				beforTaxNew = (BigDecimal) escMethod.invoke(newConfig,
						new Object[0]);
		}

		DecimalFormat df = new DecimalFormat("#.00");
		if (bs.intValue() != 0) {
			BigDecimal result = newbs.subtract(bs).divide(bs, 4, 4).multiply(
					new BigDecimal(100));
			rs[0] = new BigDecimal(df.format(result));
		}
		if (beforTax.intValue() != 0) {
			BigDecimal result = beforTaxNew.subtract(beforTax).divide(beforTax,
					4, 4).multiply(new BigDecimal(100));
			rs[1] = new BigDecimal(df.format(result));
		}
		return rs;
	}

	public List getCompaItemsById(String id, String curOrpro) {
		String hasAuth = DWRUtil.checkAuth("searchBatchCompaplan", "execute");
		if ("error".equals(hasAuth)) {
			return null;
		} else {
			ICompaplanBo compaplanBo = (ICompaplanBo) getBean("compaplanBo");
			return compaplanBo.getCompaItemsById(id, curOrpro);
		}
	}

	public Empsalaryadj getEsa() {
		return esa;
	}

	public void setEsa(Empsalaryadj esa) {
		this.esa = esa;
	}

	public Empsalaryconfig getEsc() {
		return esc;
	}

	public void setEsc(Empsalaryconfig esc) {
		this.esc = esc;
	}

	public String getCompaplanIds() {
		return compaplanIds;
	}

	public void setCompaplanIds(String compaplanIds) {
		this.compaplanIds = compaplanIds;
	}

	public String getCurEffDate() {
		return curEffDate;
	}

	public void setCurEffDate(String curEffDate) {
		this.curEffDate = curEffDate;
	}

	public String getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	}

	private Empsalaryadj esa;
	private Empsalaryconfig esc;
	private String curEffDate;
	private String compaplanIds;
	private String employeeId;
}
